package cn.xiaobai.service;

import cn.xiaobai.domain.Message;
import cn.xioabai.common.utility.ScannerUtil;
import org.springframework.boot.CommandLineRunner;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 抽象应用公共方法
 *
 * @Author yangdaji
 * @Date 2023/4/27 0027
 * @PackageName cn.xiaobai.service
 * @ClassName AbstractApplication
 */
public abstract class AbstractApplication implements CommandLineRunner {

    /**
     * 优先级信息Map key: 优先级1,2; value: 显示信息
     */
    protected static Map<Integer, List<Message>> priorityMessageMap = new HashMap<>(6);

    static {
        priorityMessageMap.put(1, new ArrayList<>());
        priorityMessageMap.put(2, new ArrayList<>());
    }

    /**
     * 文本扫描对象
     */
    protected static Scanner sc = ScannerUtil.getScanner();

    /**
     * 初始化1,2 优先级,并给空容器给值
     */
    @Override
    public void run(String... args) throws Exception {
        showMessage(1);
    }

    /**
     * 显示信息, 初始化时只显示第一优先级信息
     *
     * @param priority 优先级
     */
    public void showMessage(Integer priority) {
        List<Message> messages = priorityMessageMap.get(priority);
        messages.forEach(System.out::print);
    }

    /**
     * 监听输入
     */
    abstract void listenerInput();

    /**
     * 获取信息的序列号, 根据信息优先级
     *
     * @param priority 优先级
     * @return
     */
    List<Integer> getMessageNumber(Integer priority) {
        if (priorityMessageMap.containsKey(priority)) {
            return priorityMessageMap.get(priority).stream()
                    .map(Message::getNumber)
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }


}
